<template>
  <div>
    <H_Edit></H_Edit>
    <div class="e-content">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="标题" prop="title">
          <el-input v-model="ruleForm.title"></el-input>
        </el-form-item>

        <el-form-item label="摘要" prop="description">
          <el-input type="textarea" v-model="ruleForm.description"></el-input>
        </el-form-item>
<!--        <el-form-item label="封面" prop="img" ref="upload">
          <el-upload
              action="#"
              list-type="picture-card"
              :auto-upload="false"
              :accept="'image/*'">
            <i class="el-icon-plus"></i>

            <template #file="{file}">
              <img class="el-upload-list__item-thumbnail" :src="file.url" alt="素材封面"/>
            </template>
          </el-upload>
        </el-form-item>-->
        <el-form-item label="内容" prop="content">
          <mavon-editor v-model="ruleForm.content"></mavon-editor>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">发表</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <Footer></Footer>
  </div>
</template>

<script>
import Header from "@/components/Header";
import Element from 'element-ui'
import Footer from "@/components/Footer";
import H_Edit from "@/components/H_Edit";

export default {
  name: "BlogEdit",
  components: {H_Edit, Footer, Header},
  data() {
    return {
      ruleForm: {
        id: '',
        title: '',
        description: '',
        content: '',
        status: null,
      },
      rules: {
        title: [
          {required: true, message: '请输入标题', trigger: 'blur'},
          {min: 3, max: 25, message: '长度在 3 到 25 个字符', trigger: 'blur'}
        ],
        description: [
          {required: true, message: '请输入摘要', trigger: 'blur'}
        ],
        content: [
          {required: true, message: '请输入内容', trigger: 'blur'}
        ],
        /*img: [
          {required: true, message: '请上传图片'}
        ]*/
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const _this = this
          _this.$axios.post("/blog/edit", _this.ruleForm, {
            headers: {
              "Authorization": localStorage.getItem("token")
            }
          }).then(res => {
            Element.Message.success("发表文章成功", {duration: 3 * 1000})
            _this.$router.push("/blogs")

          })
        } else {
          Element.Message.error("提交失败", {duration: 3 * 1000})
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },

  },
  created() {
    const blogId = this.$route.params.blogId
    const _this = this
    if (blogId) {
      this.$axios.get("/blog/" + blogId).then(res => {
        const blog = res.data.data

        _this.ruleForm.id = blog.id
        _this.ruleForm.title = blog.title
        _this.ruleForm.description = blog.description
        _this.ruleForm.content = blog.content

      })
    }
  }
}
</script>

<style scoped>
.e-content {
  text-align: center;
}
</style>